
// 日历
export function date() {
  let dateObj = new Date();
  let year = dateObj.getFullYear();
  let month =
    dateObj.getMonth() < 10
      ? "0" + (dateObj.getMonth() + 1)
      : dateObj.getMonth() + 1;
  let day;
  switch (dateObj.getDay()) {
    case 0:
      day = "星期天";
      break;
    case 1:
      day = "星期一";
      break;
    case 2:
      day = "星期二";
      break;
    case 3:
      day = "星期三";
      break;
    case 4:
      day = "星期四";
      break;
    case 5:
      day = "星期五";
      break;
    case 6:
      day = "星期六";
  }
  let date =
    dateObj.getDate() < 10 ? "0" + dateObj.getDate() : dateObj.getDate();
  return { year, month, date, day };
}
// 时间
export function time() {
  let dateObj = new Date();
  let hours =
    dateObj.getHours() < 10 ? "0" + dateObj.getHours() : dateObj.getHours();
  // console.log(dateObj.getHours().length);
  let minutes =
    dateObj.getMinutes() < 10
      ? "0" + dateObj.getMinutes()
      : dateObj.getMinutes();
  let seconds =
    dateObj.getSeconds() < 10
      ? "0" + dateObj.getSeconds()
      : dateObj.getSeconds();
  return { hours, minutes, seconds };
}
// 天气
import axios from 'axios'
export function getWeather() {
  let key = "ff87cfd80e9844ac89b9a5b4216ff0dc";
  // 根据ip地址获取
  return new Promise((resolve, reject) => {
    axios.get('https://restapi.amap.com/v3/ip?key=5a80655d32342b57c0639bcbd6a40966').then(
      res=>{
        if (res.data.status === "1") {
          axios.get(`https://geoapi.qweather.com/v2/city/lookup?key=${key}&location=${res.data.city}`).then(
            res=>{
              if (res.data.code === "200") {
                let URL = `https://devapi.qweather.com/v7/weather/now?key=${key}&location=${res.data.location[0].id}`;
                axios.get(URL).then(res=>{
                  if (res.data.code === "200") {
                    resolve(res.data.now);
                  }
                })
              }
            }
          )
        }
      }
    ).catch(err=>{
      reject(err)
    })
  });

  // 根据城市获取locationId
  // return new Promise((resolve, regect) => {
  //   $.ajax({
  //     url: `https://geoapi.qweather.com/v2/city/lookup?key=${key}&location=北京市`,
  //     type: "GET",
  //     success: (res) => {
  //       if (res.code === "200") {
  //         let URL = `https://devapi.qweather.com/v7/weather/now?key=${key}&location=${res.location[0].id}`;
  //         $.get(URL, (res) => {
  //           if (res.code === "200") {
  //             resolve(res.now);
  //           }
  //         });
  //       }
  //     },
  //   });
  // });
}

